'use strict';
const { Model } = require('sequelize');
const moment = require('moment');
module.exports = (sequelize, DataTypes) => {
    class User extends Model {
        /**
         * Helper method for defining associations.
         * This method is not a part of Sequelize lifecycle.
         * The `models/index` file will call this method automatically.
         */
        //静态方法中的this和模型类User等价
        static associate(models) {
            this.hasMany(models.Role, {
                foreignKey: {
                    name: 'user_id',
                    allowNull: true,
                }
            });
            let _this = this;
            models.Role.belongsTo(_this, {
                foreignKey: {
                    name: 'user_id',
                    allowNull: true,
                }
            });
        }
    }
    User.init({
        user_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            unique: true,
        },
        username: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        nickname: {
            type: DataTypes.STRING,
            allowNull: true
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        avator: {
            type: DataTypes.STRING,
            allowNull: true
        },
        sex: {
            type: DataTypes.STRING,
            allowNull: true
        },
        email: {
            type: DataTypes.STRING,
            allowNull: true,
            // defaultValue: '1991991199@qq.com',
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            get() {
                const rawValue = this.getDataValue('createdAt');
                return moment(rawValue).utcOffset("+08:00").format('YYYY-MM-DD HH:mm:ss');
            }
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            get() {
                const rawValue = this.getDataValue('updatedAt');
                return moment(rawValue).utcOffset("+08:00").format('YYYY-MM-DD HH:mm:ss');
            }
        },
    }, {
        sequelize,
        modelName: 'User',
    });
    return User;
};